package com.hyyt.shopjsp.phone.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.phone.pojo.PhoneHomeKeyBook;
import com.hyyt.shopjsp.phone.service.IPhoneHomeKeyBookService;
import com.hyyt.shopjsp.util.common.StringStaticKey;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * @author 刘超 on 2017//30.
 */

@Controller
@RequestMapping("/back/phoneKeyBook")
public class PhoneHomeKeyBookController extends BaseController {
    private Logger log = Logger.getLogger(PhoneHomeKeyBookController.class);
    private String basePath = "/back/phone/phoneKeyBook/";

    @Resource
    private IPhoneHomeKeyBookService phoneHomeKeyBookService;//数据字典service接口

    /**
     * 进入数据字典管理
     *
     * @return 进入指定页面
     */
    @RequestMapping("gotoPhoneKeyBookPage")
    public String gotoPhoneKeyBookPage() {
        return basePath + "phoneKeyBookList";
    }

    /**
     * 查询所有数据字典
     *
     * @param page
     * @param response
     * @param request
     */
    @RequestMapping(value = "listPhoneKeyBook", method = RequestMethod.POST)
    @ResponseBody
    public void listPhoneKeyBook(Page page, HttpServletResponse response, HttpServletRequest request) {
        String name = request.getParameter("name");//名称
        String typeName = request.getParameter("typeName");//类型名称
        Map<String, Object> param = new HashMap<>();
        if (Utils.objectIsNotEmpty(name)) {//是否检索名称
            param.put("name", name.trim());
        }
        if (Utils.objectIsNotEmpty(typeName)) {//是否检索类型名称
            param.put("typeName", typeName.trim());
        }
        param.put("sortColumns", "homeKeyBookId asc");
        int totalRecordCount = phoneHomeKeyBookService.countPhoneHomeKeyBook(param);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<PhoneHomeKeyBook> phoneHomeKeyBookList = phoneHomeKeyBookService.queryPhoneHomeKeyBook(param, page.getPageRecordBeginIndex(), page.getPageSize());
        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", phoneHomeKeyBookList);// rows键 存放每页记录 list
        ResultMsg.create(param, response);  //把内容发送到页面
    }

    /**
     * 根据数据字典id获得对象
     *
     * @param homeKeyBookId
     */
    @RequestMapping(value = "getPhoneKeyBookInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getPhoneKeyBookInfo(Integer homeKeyBookId) {
        if (Utils.objectIsNotEmpty(homeKeyBookId)) {
            PhoneHomeKeyBook phoneHomeKeyBook = phoneHomeKeyBookService.loadPhoneHomeKeyBook(homeKeyBookId);
            return ResultMsg.create(true, null, FastJsonUtils.toJSONString(phoneHomeKeyBook));
        }
        return ResultMsg.create(false);
    }

    /**
     * 保存或者修改数据字典
     *
     * @param phoneHomeKeyBook
     * @return
     */
    @RequestMapping(value = "saveOrUpdatePhoneKeyBook", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdatePhoneKeyBook(PhoneHomeKeyBook phoneHomeKeyBook) {
        if (Utils.objectIsNotEmpty(phoneHomeKeyBook)) {
            if (Utils.objectIsEmpty(phoneHomeKeyBook.getHomeKeyBookId())) {
                phoneHomeKeyBookService.savePhoneHomeKeyBook(phoneHomeKeyBook);
            } else {
                phoneHomeKeyBookService.updatePhoneHomeKeyBook(phoneHomeKeyBook);
            }
        }
        return ResultMsg.create(true);
    }

    /**
     * 删除传入的数据字典id
     *
     * @param homeKeyBookId
     */
    @RequestMapping(value = "deletePhoneKeyBook", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deletePhoneKeyBook(Integer homeKeyBookId) {
        if (Utils.objectIsNotEmpty(homeKeyBookId)) {
            phoneHomeKeyBookService.deletePhoneHomeKeyBook(homeKeyBookId);
        }
        return ResultMsg.create(true);
    }

    /**
     * 同步手机数据字典
     *
     */
    @RequestMapping(value = "initPhoneHomeKeyBookDataInfo")
    @ResponseBody
    public ResultMsg initPhoneHomeKeyBookDataInfo() {
        Map<String, Object> param = new HashMap<>();
        ResultMsg resultMsg = new ResultMsg(false);
        boolean success = false;
        List<String> typeList = phoneHomeKeyBookService.getStatusRelateType();//查询要加载的数据（通过type进行了去重）
        if (Utils.collectionIsNotEmpty(typeList)) {
            for (String type : typeList) {
                param.put("type", type);
                List<PhoneHomeKeyBook> phoneKeyBookList = phoneHomeKeyBookService.queryPhoneHomeKeyBook(param);   //通过type查询对应的集合
                if (Utils.collectionIsNotEmpty(phoneKeyBookList)) {
                    String jsonString = FastJsonUtils.toJSONString(phoneKeyBookList);//将value格式化成json格式
                    redisBasicService.setKey(StringStaticKey.PHONE_KEYBOOK_PREFIX + type, jsonString); //将查询结果放入redis
                }
            }
            success = true;//查询到数据 将result更改为true
            resultMsg.setSuccess(success);
            resultMsg.setMsg("生成数据手机字典数据成功!");
        }
        return resultMsg;  //把内容发送到页面
    }

}